define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/shenuser/index',
                    // add_url: 'user/shenuser/add',
                    // edit_url: 'user/shenuser/edit',
                    del_url: 'user/shenuser/del',
                    multi_url: 'user/shenuser/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), sortable: true },
                        // {field: 'group.name', title: __('Group')},
                        // {field: 'username', title: __('Username'), operate: 'LIKE'},
                        { field: 'nickname', title: __('Nickname'), operate: 'LIKE' },
                        // {field: 'email', title: __('Email'), operate: 'LIKE'},
                        { field: 'avatar', title: __('头像'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },
                        { field: 'mobile', title: __('手机号'), operate: 'LIKE' },
                        // {field: 'level', title: __('Level'), operate: 'BETWEEN', sortable: true},
                        // {field: 'gender', title: __('Gender'), visible: false, searchList: {1: __('Male'), 0: __('Female')}},
                        // {field: 'score', title: __('Score'), operate: 'BETWEEN', sortable: true},
                        // {field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true},
                        { field: 'username', title: __('姓名'), operate: 'LIKE' },
                        { field: 'idcard', title: __('身份证号'), operate: 'LIKE' },
                        { field: 'idfront_image', title: __('身份证正面'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'idback_image', title: __('身份证反面'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'authen_time', title: __('提交时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true },
                        { field: 'shen_time', title: __('审核时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true },
                        // { field: 'loginip', title: __('登录ip'), formatter: Table.api.formatter.search },
                        // { field: 'logintime', title: __('登录时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true },
                        // {field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        // {field: 'joinip', title: __('Joinip'), formatter: Table.api.formatter.search},
                        { field: 'stutes', title: __('认证状态'), formatter: Table.api.formatter.status, searchList: { 0: '未认证', 1: '待审核', 2: '已认证', 3: '未通过' } },
                        {
                            field: 'operate', title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'handle',
                                    text: '审核',
                                    title: '审核',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-check',
                                    url: 'user/shenuser/handle',
                                    callback: function (data) {
                                        Layer.alert(data.msg);
                                        // 刷新表格
                                        table.bootstrapTable('refresh');
                                    }
                                }
                            ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        handle: function () {
            $(function () {
                // 初始显示/隐藏原因框
                toggleReasonBox();

                // 监听审核状态变化
                $('#audit-status').change(function () {
                    toggleReasonBox();
                });

                function toggleReasonBox() {
                    if ($('#audit-status').val() == '3') {
                        $('#reason-group').show();
                    } else {
                        $('#reason-group').hide();
                    }
                }

                // 表单提交处理
                $('#audit-form').submit(function (e) {
                    e.preventDefault();
                    var form = $(this);
                    Fast.api.ajax({
                        url: form.attr('action'),
                        type: 'post',
                        data: form.serialize(),
                        success: function (data) {
                            Layer.alert(data.msg, function () {
                                // 关闭当前弹窗并刷新表格
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                parent.$('#table').bootstrapTable('refresh');
                            });
                        }
                    });
                    return false;
                });
            });
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});